const style = (el, prop) => {
    return typeof getComputedStyle !== 'undefined' ? getComputedStyle(el, null).getPropertyValue(prop) : el.style[prop];
  };
  
  const overflow = el => {
    return style(el, 'overflow') + style(el, 'overflow-y') + style(el, 'overflow-x');
  };
  
  const scrollParent = el => {
    if (!(el instanceof HTMLElement)) {
      return window;
    }
  
    let parent = el;
    while (parent) {
      if (parent === document.body || parent === document.documentElement) {
        break;
      }
  
      if (!parent.parentNode) {
        break;
      }
  
      if (/(scroll|auto)/.test(overflow(parent))) {
        return parent;
      }
      parent = parent.parentNode;
    }
  
    return window;
  };
  
  function throttle(action, delay) {
    let timeout = null;
    let lastRun = 0;
    return function(...args) {
      if (timeout) {
        return;
      }
      const elapsed = Date.now() - lastRun;
      const context = this;
      // const args = arguments;
      const runCallback = function() {
        lastRun = Date.now();
        timeout = false;
        action.apply(context, args);
      };
      if (elapsed >= delay) {
        runCallback();
      } else {
        timeout = setTimeout(runCallback, delay);
      }
    };
    // let pre = Date.now();
    // return function(...args) {
    //   const context = this;
    //   const now = Date.now();
    //   if (now - pre > delay) {
    //     action.apply(context, args);
    //     pre = Date.now();
    //   }
    // };
  }
  
  function testSupportsPassive() {
    let support = false;
    try {
      const opts = Object.defineProperty({}, 'passive', {
        get() {
          support = true;
          return '';
        }
      });
      window.addEventListener('test', null, opts);
    } catch (e) {
      console.log(e);
    }
    return support;
  }
  
  const supportsPassive = testSupportsPassive();
  
  const _ = {
    on(el, type, func, capture = false) {
      if (supportsPassive) {
        el.addEventListener(type, func, {
          capture,
          passive: true
        });
      } else {
        el.addEventListener(type, func, capture);
      }
    },
    off(el, type, func, capture = false) {
      el.removeEventListener(type, func, capture);
    }
  };
  function nextTick(fn) {
    setTimeout(fn, 0);
  }
  
  export default {
    _,
    nextTick,
    throttle,
    scrollParent
  };